import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { RoleService } from './role.service';
import {
  PageSearchRoleDto,
  RoleDto,
  getMenuDto,
  RoleListDto,
} from '../../dto/role/role.dto';
import { Role } from '../../entities/role.entity';
import { Public } from '../../common/decorators/public.decorator';
import { PaginatedResponseDto } from 'src/common/class/res.class';
import { MenuService } from '../menu/menu.service';

@ApiTags('角色')
@ApiBearerAuth()
@Controller('role')
export class RoleController {
  constructor(
    private readonly roleService: RoleService,
    private readonly menuService: MenuService,
  ) {}

  @ApiOperation({ summary: '创建角色' })
  @ApiBody({ type: RoleListDto, description: '输入角色的各种信息' })
  @Post('create')
  async create(@Body() roleListDto: RoleListDto): Promise<Role> {
    return await this.roleService.create(roleListDto);
  }
  @ApiOperation({ summary: '分页条件查询角色信息' })
  @ApiOkResponse({ type: RoleListDto, description: '分页查询结果' })
  @Get('page')
  async page(
    @Query() dto: PageSearchRoleDto,
  ): Promise<PaginatedResponseDto<Role>> {
    const [records, total] = await this.roleService.page(dto);

    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }
  @ApiOperation({ summary: '通过ID获取角色信息' })
  @ApiParam({ name: 'id', required: true, description: '角色的ID' })
  @Get()
  async findOne(@Param('id') id: string): Promise<Role> {
    return await this.roleService.findOne(id);
  }

  @ApiOperation({ summary: '更新角色信息' })
  @ApiBody({ type: RoleListDto, description: '输入更新的角色信息' })
  @Post('edit')
  async update(@Body() updateRoleDto: RoleListDto): Promise<Role> {
    return await this.roleService.update(updateRoleDto);
  }

  @ApiOperation({ summary: '删除角色' })
  @ApiParam({ name: 'id', required: true, description: '角色的ID' })
  @Delete('delete')
  async remove(@Param('id') id: string): Promise<void> {
    return await this.roleService.remove(id);
  }

  @ApiOperation({ summary: '查询所有角色' })
  @Get('queryall')
  async findAll(): Promise<any> {
    const data = await this.roleService.findAll();
    return data;
  }

  @ApiOperation({ summary: '角色关联菜单' })
  @Put('permission/saveRolePermission')
  @ApiBody({ type: getMenuDto, description: '角色关联菜单' })
  async associateMenuWithRole(@Body() getMenuDto: getMenuDto): Promise<void> {
    return await this.roleService.associateMenuWithRole(
      getMenuDto.roleId,
      getMenuDto.menuIds,
    );
  }
  @ApiOperation({ summary: '查询角色菜单和关联的ids' })
  @ApiParam({ name: 'roleId', required: true, description: '角色的ID' })
  @Get('queryTreeList')
  async getMenusByRoleId(@Query('roleId') roleId: string): Promise<any> {
    console.log(roleId, 'roleId');

    const [ids, treeList] = await this.roleService.getMenusByRoleId(roleId);
    console.log(ids, treeList);
    return {
      ids,
      treeList,
    };
  }
}
